<?php


namespace app\api\validate;


use think\Db;
use think\Validate;

class ServiceProviderApply extends Validate
{
    protected $rule = [
        'user_id' => 'apply',
        'type' => 'require|in:1,2', // 1-个人, 2-企业
        'real_name' => 'require',
        'phone' => 'require|mobile',
        'province' => 'require|number',
        'city' => 'require|number',
        'district' => 'require|number',
        'address' => 'require',
        'id_card' => 'require|idCard',
        // 企业认证特有字段
        'company_name' => 'requireIf:type,2',

        'admin_name' => 'requireIf:type,2',
        'admin_mobile' => 'requireIf:type,2|mobile',
    ];

    protected $message = [
        'real_name.require' => '请填写真实姓名',
        'phone.require' => '请填写手机号码',
        'phone.mobile' => '请填写正确的手机号码',
        'province.province' => '请填写省份',
        'city.city' => '请填写城市',
        'district.district' => '请填写县区',
        'address.require' => '请填写详细地址',
        'type.require' => '请填写申请类型',
        'type.in' => '申请类型错误',
        // 个人认证
        'id_card.require' => '请填写身份证号',
        'id_card.idCard' => '请填写正确的身份证号',
        // 企业认证
        'company_name.requireIf' => '请填写企业名称',
        'admin_name.requireIf' => '请填写管理员姓名',
        'admin_mobile.requireIf' => '请填写管理员手机号',
        'admin_mobile.phone' => '请填写正确的管理员手机号',
    ];


    protected function apply($user_id, $other, $data)
    {
        $result = Db::name('user_service')
            ->where('user_id', $user_id)
            ->where('is_verified', 0)
            ->find();
        if ($result) {
            return '正在审核中，请勿重复提交';
        }
        return true;
    }
}